home *** CD-ROM | disk | FTP | other *** search
- Path: ix.netcom.com!news
- From: miker3@ix.netcom.com (Mike Rubenstein)
- Newsgroups: comp.lang.c
- Subject: Re: Settle a bet please
- Date: Sun, 31 Mar 1996 17:12:11 GMT
- Organization: Netcom
- Message-ID: <315ebb0b.443785980@nntp.ix.netcom.com>
- References: <4jfopb$o9n@news1.sympatico.ca> <KASPER.96Mar29073654@acm.org> <mjs.828111568@hubcap> <KASPER.96Mar30085147@acm.org>
- NNTP-Posting-Host: ix-dc13-29.ix.netcom.com
- X-NETCOM-Date: Sun Mar 31 11:14:49 AM CST 1996
- X-Newsreader: Forte Agent .99d/32.182
-
- kasperowski@acm.org wrote:
-
- > In article <mjs.828111568@hubcap> mjs@hubcap.clemson.edu (M. J. Saltzman) writes:
- >
- > | kasperowski@acm.org writes:
- > |
- > | > char Name[8] = "My Name";
- > |
- > | >is correct. There's a \0 at the end of "My Name".
- > |
- > | > char Name[] = "My Name";
- > |
- > | >is better; the compiler does the counting for you.
- > |
- > | Actually, all three are correct, they just mean different things. First,
- > |
- > | char name[] = "My name";
- > |
- > | and
- > |
- > | char name[8] = "My name";
- > |
- > | do mean the same thing: reserve 8 chars and initialize them to contain
- > |
- > | {'M', 'y', ' ', 'n', 'a', 'm', 'e', '\0'}.
- > |
- > | This means that name[] can be treated as a "C string" (i.e., a
- > | null-terminated string) and used with the C string and i/o library
- > | routines that understand about null-terminated strings.
- > |
- > | But
- > |
- > | char name[7] = "My name";
- > |
- > | reserves 7 chars, and initializes them to contain
- > |
- > | {'M', 'y', ' ', 'n', 'a', 'm', 'e'}.
- > |
- > | This is perfectly legal C, but the resulting array of characters is
- >
- > In private e-mail, someone said the same thing to me, and I replied,
- > "sure, I guess you're right." I was wrong -- this is illegal.
- >
- > In ANSI C,
- >
- > char name[7] = "My name";
- >
- > which is the same as
- >
- > char name[7] = {'M', 'y', ' ', 'n', 'a', 'm', 'e', '\0'};
- >
- > is *not* legal. Section A8.7 on p. 219 of Appendix A of my copy of
- > K&R's _The C Programming Language_, 2nd ed., says:
- >
- > If the array has fixed size, the number of initializers may not
- > exceed the number of members of the array . . .
- >
- > The line specifies an array of fixed size 7 chars, and then
- > initializes it with 8 chars. This is illegal.
-
- But the standard says otherwise. From ISO 6.5.7:
-
- An array of character type may be initialized by a character
- string literal, optionally enclosed in braces. Successive
- characters of the character string literal (including the
- terminating null character if there is room or if the array is
-
- of unknown size) initialze the elements of the array.
-
- There also is an example (note: examples are not part of the
- standard):
-
- 7. The declaration
-
- char s[] = "abc", t[3] = "abc";
-
- defines "plain" chara rray objects s and t whose elements
- are initialized with character string literals. This
- declaration is identical to
-
- char s[] = { 'a', 'b', 'c', '\0' },
- t[] = { 'a', 'b', 'c' };
-
-
- Michael M Rubenstein
-